define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        list: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yalimedia.question.detail/list' + location.search + '&ids='+$('#ids').val(),
                    // add_url: 'yalimedia.question.detail/add',
                    // edit_url: 'yalimedia.question.detail/edit',
                    // del_url: 'yalimedia.question.detail/del',
                    // multi_url: 'yalimedia.question.detail/multi',
                    // import_url: 'yalimedia.question.detail/import',
                    table: 'yalimedia_question_log_detail',
                }
            });

            var table = $("#table");
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search:false,
                columns: [
                    [
                        {field: 'id', title: __('题目ID')},
                        {field: 'kind_text', title: __('题目类型')},
                        {field: 'material_title', title: __('材料题目'),
                            formatter : function(value, row, index, field){
                                if(!value){
                                    return '-';
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + value + "'>" + value + "</span>";
                            },
                            // 固定列最大宽度，超出隐藏
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        {field: 'title', title: __('题目'),
                            formatter : function(value, row, index, field){
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;' title='" + value + "'>" + value + "</span>";
                            },
                            // 固定列最大宽度，超出隐藏
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        {
                            field: 'answer', title: __('正确答案'), halign: 'center', align: 'left', operate: false, formatter: function (value, row, index) {
                                var answer = value;
                               
                                // 填空题
                                if (row.kind == 'FILL') {
                                    try {
                                        answer = '';
                                   
                                        // let fill_answers = JSON.parse(row.answer);
                                        let fill_answers = row.answer;
                                       
                                        for (let i = 0; i < fill_answers.length; i++) {
                                            answer += '填空位：' + fill_answers[i].answers.join('、') + '<br>';
                                        }
                                    } catch (e) {
                                        return value;
                                    }
                                } else if (row.kind == 'SHORT') {
                                    try {
                                        let short_answers = (row.answer);
                                        answer = '标准答案：' + short_answers.answer + '<br>';
                                        for (let i = 0; i < short_answers.config.length; i++) {
                                            // answer += '关键词' + (i + 1) + '：' + short_answers[i].answer + '<br>';
                                            answer += '关键词' + (i + 1) + '：' + short_answers.config[i].answer + '<br>';
                                        }
                                    } catch (e) {
                                        return value;
                                    }
                                } else if (row.kind == 'CALL' ||  row.kind == 'MATERIAL') {
                                    return '-';
                                }else if (row.kind == 'WORD' ) {
                                    return '-';
                                }
                                
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;'>" + answer + "</span>";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        },
                        {
                            field: 'user_answer', title: __('学员答案'), halign: 'center', align: 'left', operate: false, formatter: function (value, row, index) {

                                var answer = row.user_answer;
                                if(answer.length == 0){
                                    return '<span class="text-red">未答</span>';
                                }
                                
                                var answer = answer.answer;
                                // 填空题
                                if (row.kind == 'FILL') {

                                    try {

                                        answer = '';
                                        let fill_answers = row.user_answer.answer;
                                        
                                        if( typeof fill_answers == 'string'){
                                            return fill_answers;
                                        }
                                     
                                        for (let i = 0; i < fill_answers.length; i++) {
                                            if(fill_answers[i].value != ''){
                                                answer += '填空位：' + fill_answers[i].value + '<br>';
                                            }
                                        }

                                    } catch (e) {
                                        return value;
                                    }

                                } else if (row.kind == 'SHORT') {
                                    
                                    try {
                                    
                                        if(row.answer.answer){
                                            return row.answer.answer;
                                        }
                                        
                                    } catch (e) {
                                        return value;
                                    }

                                } else if ( row.kind == 'WORD' ) {

                                    answer = '';

                                    if(!row.user_answer){
                                        return '<span class="text-red">未答</span>';
                                    }

                                    let word_answers = row.user_answer.xunfeiResMsg;
                                    if(!word_answers){
                                        return '<span class="text-red">未答</span>';
                                    }
                                    console.log(word_answers.length);
                                    for (let i = 0; i < word_answers.length; i++) {
                                        console.log('======',word_answers[i].singleRecord);
                                        // answer += '语音' + (i + 1) + '：' + word_answers[i].singleRecord + '<br>';
                                        answer += '<audio src="'+word_answers[i].singleRecord+'" controls>语音' + (i + 1)+'</audio> <br>';
                                    }
                                 
                                }else if (row.kind == 'CALL'  || row.kind == 'MATERIAL') {

                                    return '-';
                                }

                                if(answer == ''){
                                    answer = '<span class="text-red">未答</span>';
                                }
                                return "<span style='display: block;overflow: hidden;text-overflow: ellipsis;white-space: nowrap;'>" + answer + "</span>";
                            },
                            cellStyle : function(value, row, index, field){
                                return {
                                    css: {
                                        "white-space": "nowrap",
                                        "text-overflow": "ellipsis",
                                        "overflow": "hidden",
                                        "max-width":"200px"
                                    }
                                };
                            }
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
